package com.terios.http;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.weibo.net.WeiboException;

/**
 * JsonAnalysis.java 2012-10-4 下午3:10:51
 * 
 * @author Wang Tengrui
 */
public class JsonAnalysis
{
	private static Map<String, SimpleDateFormat> formatMap = new HashMap<String, SimpleDateFormat>();

	public JsonAnalysis()
	{
	}

	/**
	 * 从responseString创建json对象
	 */
	protected static JSONObject getJSONObject(String responseString)
			throws WeiboException
	{
		JSONObject jsonObject = null;
		try
		{
			jsonObject = new JSONObject(responseString);
		} catch (JSONException jsone)
		{
			throw new WeiboException(
					"无法创建json对象，请检查responseString格式是否符合json数据格式", jsone);
		}
		return jsonObject;
	}

	/**
	 * 从json对象中获取json数组
	 * 
	 * @throws JSONException
	 */
	protected static JSONArray getJSONArray(String key, JSONObject jsonObject)
			throws JSONException
	{
		JSONArray jsonArray = null;
		jsonArray = jsonObject.getJSONArray(key);
		return jsonArray;
	}

	/**
	 * 检测json对象中是否有需要的key
	 */
	protected static boolean ensureKeyIs(String key, JSONObject jsonObject)
	{
		@SuppressWarnings("unchecked")
		Iterator<String> keyIterator = jsonObject.keys();
		for (Iterator<String> iterator = keyIterator; iterator.hasNext();)
		{
			if (key.equals(iterator.next()))
			{
				return true;
			}
		}
		return false;
	}

	protected static int getInt(String key, JSONObject json)
			throws JSONException
	{
		String str = json.getString(key);
		if (null == str || "".equals(str) || "null".equals(str))
		{
			return -1;
		}
		return Integer.parseInt(str);
	}

	protected static long getLong(String key, JSONObject jsonObject)
			throws JSONException
	{
		String str = jsonObject.getString(key);
		if (null == str || "".equals(str) || "null".equals(str))
		{
			return -1;
		}
		return Long.parseLong(str);
	}

	protected static boolean getBoolean(String key, JSONObject json)
			throws JSONException
	{
		String str = json.getString(key);
		if (null == str || "".equals(str) || "null".equals(str))
		{
			return false;
		}
		return Boolean.valueOf(str);
	}

	/**
	 * 解码json中获取的数据
	 */
	protected static String getString(String name, JSONObject json,
			boolean decode)
	{
		String returnValue = null;
		try
		{
			returnValue = json.getString(name);
			if (decode)
			{
				try
				{
					returnValue = URLDecoder.decode(returnValue, "UTF-8");
				} catch (UnsupportedEncodingException e)
				{
					// TODO: handle exception
				}
			}
		} catch (JSONException e)
		{
			// TODO: handle exception
		}
		return returnValue;
	}

	/**
	 * 时间格式转换
	 */
	protected static Date parseDate(String str, String format)
			throws WeiboException
	{
		if (str == null || "".equals(str))
		{
			return null;
		}
		SimpleDateFormat sdf = formatMap.get(format);
		if (null == sdf)
		{
			sdf = new SimpleDateFormat(format,Locale.ENGLISH);
			sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
			formatMap.put(format, sdf);
		}
		try
		{
			synchronized (sdf)
			{
				// SimpleDateFormat is not thread safe
				return sdf.parse(str);
			}
		} catch (ParseException pe)
		{
			throw new WeiboException("Unexpected format(" + str
					+ ") returned from sina.com.cn");
		}
	}
}
